<?php



namespace app\bbxw\controller\api;



use app\bbxw\service\WechatService;
use app\bbxw\service\UserAdminService;
use think\admin\Controller;
use app\bbxw\model\WxUser as WxUserMod;
use think\admin\extend\CodeExtend;
use think\admin\model\SystemUser;

/**
 * 用户接口
 * Class Login
 * @package app\data\controller\api
 */
class Customer extends Controller
{
    //首页数据
    public function index()
    {
        $customer_id = $this->request->get('id');
        $customer = $this->app->db->name("customer")->where(['id'=>$customer_id])->find();
        $work_hours = $this->app->db->name("work_hours")
            ->where("star_date","<",date("H:i:s"))->where("end_date",">",date("H:i:s"))->select()->toArray();
        $grade_image = $this->app->db->name("membership_card")->where(['id'=>$customer['grade']])->value("image");
        $this->success('获取成功',['customer'=>$customer,'work_hours'=>isset($work_hours[0])?$work_hours[0]:[],'grade_image'=>$grade_image]);
    }

    public function mindinfo()
    {
        $customer_id = $this->request->get('id');
        $customer = $this->app->db->name("customer")->where(['id'=>$customer_id])->find();
        $grade = $this->app->db->name("membership_card")->where(['id'=>$customer['grade']])->find();
        $this->success('获取成功',['customer'=>$customer,'grade'=>$grade]);
    }

    public function customer()
    {
        $customer_id = $this->request->get('id');
        $customer = $this->app->db->name("customer")->where(['id'=>$customer_id])->find();
        $customer['balance'] = bcsub((string)$customer['prestore_total_amount'],(string)$customer['use_total_amount'],2);
        $customer['balance'] = "--";
//        if($customer['balance']<0){
//            $customer['balance'] = 0.00;
//        }
        $this->success('获取成功',$customer);
    }

    public function orderlist()
    {
        $customer_id = $this->request->get('id');
        $customer = $this->app->db->name("customer")->where(['id'=>$customer_id])->find();
        //余额
        $customer['balance'] = $customer['prestore_total_amount']-$customer['use_total_amount'];
        $customer['balance'] = "--";
        $orderDay = $this->app->db->name("play_accompany")->where(['customer_id'=>$customer_id])->whereDay("date")->order('id desc')->select()->toArray();
        $orderWeek = $this->app->db->name("play_accompany")->where(['customer_id'=>$customer_id])->whereWeek("date")->order('id desc')->select()->toArray();
        $orderMonth = $this->app->db->name("play_accompany")->where(['customer_id'=>$customer_id])->whereMonth("date")->order('id desc')->select()->toArray();
        foreach ($orderDay as $k=>$v){
            $orderDay[$k]['date'] = date("Y-m-d",strtotime($orderDay[$k]['date']));
        }
        foreach ($orderWeek as $k=>$v){
            $orderWeek[$k]['date'] = date("Y-m-d",strtotime($orderWeek[$k]['date']));
        }
        foreach ($orderMonth as $k=>$v){
            $orderMonth[$k]['date'] = date("Y-m-d",strtotime($orderMonth[$k]['date']));
        }
        $this->success('获取成功',['customer'=>$customer,'orderDay'=>$orderDay,'orderWeek'=>$orderWeek,'orderMonth'=>$orderMonth]);
    }

    public function hyqy()
    {
        $customer_id = $this->request->get('id');
        $customer = $this->app->db->name("customer")->where(['id'=>$customer_id])->find();
        $grade = $this->app->db->name("membership_card")->where(['id'=>$customer['grade']])->find();
        // 将多行文本按换行符分割成数组
        $remarkLines = explode("\n", $grade['remark']);
        // 去除每一行的前后空格
        $remarkLines = array_map('trim', $remarkLines);
        // 去除空行
        $remarkLines = array_filter($remarkLines, function($line) {
            return !empty($line);
        });
        // 将处理后的数组赋值给 $grade['remark']
        $grade['remark_arr'] = array_values($remarkLines); // 重新索引数组
        $next_grade = $this->app->db->name("membership_card")->where(['id'=>$customer['grade']+1])->find();
        $percent = 100;
        $next_amount = 0;
        if($next_grade){
            $percent = round(($customer['use_total_amount']/$next_grade['amount'])*100,2);
            $next_amount = bcsub((string)$next_grade['amount'],(string)$customer['use_total_amount'],2);
        }
        $customer_secondary_card = $this->app->db->name("customer_secondary_card")->where(['customer_id'=>$customer_id])->select()->toArray();
        $this->success('获取成功',[
            'customer'=>$customer,
            'grade'=>$grade,
            'next_grade'=>$next_grade,
            'customer_secondary_card'=>$customer_secondary_card,
            'percent'=>$percent,
            'next_amount'=>$next_amount,
            'total_amount' => $customer['use_total_amount'],
            'prestore_total_amount'=>$customer['prestore_total_amount'],
        ]);
    }

    public function articleList()
    {
        $type = $this->request->get('type');
        $page = $this->request->get('page',1);
        $q = $this->_query('article')->where(['status'=>1,'is_deleted'=>0,'type'=>1]);
        switch ($type){
            case 1:
                $q->whereWeek("star_date")->where('end_date','>',date("Y-m-d"));
                break;
            case 2:
                $q->whereMonth("star_date")->where('end_date','>',date("Y-m-d"));
                break;
            case 3:
                $q->where('end_date','<',date("Y-m-d"));
                break;
        }

        $data = $q->order('star_date desc')->page($page,false,false,10);
        foreach ($data['list'] as &$v){
            $v['star_date'] = date("Y年m月d日",strtotime($v['star_date']));
            $v['end_date'] = date("Y年m月d日",strtotime($v['end_date']));
        }
        $this->success('获取成功',$data);
    }
    public function articleDetail()
    {
        $id = $this->request->get('id');
        $detail = $this->app->db->name("article")->where(['id'=>$id])->find();
        if($detail){
            $detail['star_date'] = date("Y年m月d日",strtotime($detail['star_date']));
            $detail['end_date'] = date("Y年m月d日",strtotime($detail['end_date']));
        }

        $this->success('获取成功',$detail);
    }

    public function naming()
    {
        $customer_id = $this->request->get('id');
        $naming = $this->app->db->name("naming")->where(['customer_id'=>$customer_id])->select()->toArray();
        $this->success('获取成功',$naming);
    }

    public function getGoods()
    {
        $goods = $this->app->db->name("goods")->order('sort ASC')->select()->toArray();
        $this->success('获取成功',$goods);
    }
    public function exchangeOrder()
    {
        $customer_id = $this->request->get('id');
        $type = $this->request->get('type');
        $list = $this->app->db->name("exchange_order")->where(['user_id'=>$customer_id,'type'=>$type])->order('id desc')->select()->toArray();
        foreach ($list as $k=>$v){
            $list[$k]['goods_info'] = json_decode($list[$k]['goods_info'],true);
        }
        $this->success('获取成功',$list);
    }

    public function exchange()
    {
        $customer_id = $this->request->get('id');
        $goods_id = $this->request->get('goods_id');
        $type = $this->request->get('type');
        $goods = $this->app->db->name("goods")->where(['id'=>$goods_id])->find();
        if($goods['stock']<=0){
            $this->error('库存不足');
        }

        $table_name= "customer";
        if($type==2){
            $table_name= "power_source";
        }
        $customer = $this->app->db->name($table_name)->where(['id'=>$customer_id])->find();
        if(($customer['points']-$customer['use_points'])<$goods['price']){
            $this->error('积分不足');
        }
        $this->app->db->name($table_name)->where(['id'=>$customer_id])->update(['use_points'=>$customer['use_points']+$goods['price']]);
        $this->app->db->name("goods")->where(['id'=>$goods_id])->update(['stock'=>$goods['stock']-1]);
        $this->app->db->name("exchange_order")->insert([
            'user_id'=>$customer_id,
            'goods_id'=>$goods_id,
            'order_no'=>CodeExtend::uniqidDate(18, 'NO'),
            'amount'=>$goods['price'],
            'customer_name'=>$customer['name'],
            'goods_info' => json_encode($goods),
            'goods_name' => $goods['title'],
            'type'=>$type
        ]);
        $this->success('兑换成功');
    }

    public function savebirthday()
    {
        $customer_id = $this->request->get('id');
        $birthday = $this->request->get('birthday');
        $this->app->db->name("customer")->where(['id'=>$customer_id])->update(['birthday'=>$birthday]);
        $this->success('保存成功');
    }

    //消费明细 按日期展示
    public function consumption()
    {
        $customer_id = $this->request->get('id');
        $customer = $this->app->db->name("customer")->where(['id'=>$customer_id])->find();
        //余额
        $customer['balance'] = $customer['prestore_total_amount']-$customer['use_total_amount'];
        $dates = [];
        for ($i = 0; $i < 7; $i++) {
            $dates[] = date('Y-m-d', strtotime("-$i day"));
        }
        $list = [];
        $weeks = ["周日", "周一", "周二", "周三", "周四", "周五", "周六"];
        $weeks_en = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"];
        foreach ($dates as $k=>$v){
            $list[$k]['business_amount'] = $this->app->db->name("play_accompany")->where(['customer_id'=>$customer_id])
                ->whereDay("date",$v)->order('id desc')->sum('business_amount');
            $list[$k]['date'] = $v;
            $week = date('w',strtotime($v));
            $list[$k]['week'] = $weeks[$week];
            $list[$k]['week_en'] = $weeks_en[$week];
        }
        $this->success('获取成功',['customer'=>$customer,'list'=>$list]);
    }

    public function dayList()
    {
        $customer_id = $this->request->get('id');
        $date = $this->request->get('date');
        $list = $this->app->db->name("play_accompany")->where(['customer_id'=>$customer_id,'status'=>1])
            ->whereDay("date",$date)->order('id desc')->select()->toArray();
        $total = $this->app->db->name("play_accompany")->where(['customer_id'=>$customer_id,'status'=>1])
            ->whereDay("date",$date)->order('id desc')->sum("business_amount");
        $this->success('获取成功',['list'=>$list,'total'=>$total]);

    }


    public function chagePassword()
    {
        $power_source_id = $this->request->post('id');
        $password = $this->request->post('password');
        $this->app->db->name("customer")->where(['id'=>$power_source_id])->update(['password'=>md5($password)]);
        $this->success('修改成功');
    }

    public function saveHeadimg()
    {
        $customer_id = $this->request->get('id');
        $harder_img = $this->request->get('harder_img');
        $this->app->db->name("customer")->where(['id'=>$customer_id])->update(['harder_img'=>$harder_img]);
        $this->success('保存成功');
    }



}
